package com.wk.springboot.intrude.itemReader;

import com.wk.springboot.bean.Player;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.DefaultFieldSet;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;

/**
 * @Classname FlatFiles
 * @Description 普通的系统文件
 * @Date 2020/4/7 14:32
 * @Author by weikai
 */
@Configuration
public class FlatFilesItemReader implements InitializingBean {

  /**
   * 系统文件对应一个java对象FieldSet
   * 他就想JDBC的resultSet一样，是读取数据的一个结果集
   * 它允许通过下标和名字获取数据
   */
  public FieldSet testFieldSet(){
    String[] tokens = new String[]{"foo", "1", "true"};
    FieldSet fs = new DefaultFieldSet(tokens);
    String name = fs.readString(0);
    int value = fs.readInt(1);
    boolean booleanValue = fs.readBoolean(2);
    System.out.println(name);
    System.out.println(value);
    System.out.println(booleanValue);
    return fs;
  }

  /**
   * 读取player信息
   * 因为如果reader里没有数据了就为空
   * @return
   * @throws Exception
   */
  public Player getPlayers() throws Exception {
    FlatFileItemReader<Player> itemReader = new FlatFileItemReader<>();
    itemReader.setResource(new ClassPathResource("players.csv"));
    DefaultLineMapper<Player> lineMapper = new DefaultLineMapper<>();
    lineMapper.setLineTokenizer(new DelimitedLineTokenizer());
    lineMapper.setFieldSetMapper(new PlayerFieldSetMapper());
    itemReader.setLineMapper(lineMapper);
    itemReader.open(new ExecutionContext());
    List<Player> list = new LinkedList();
    Player player = new Player();
    /**
     * read都是按照行读取的
     * 再没有数据的时候，就会返回null
     */
    while(player != null){
      player = itemReader.read();
      list.add( player);
    }
    return player;
  }

  /**
   * 获得FlatItemReader
   * @return
   */
  @Bean
  public FlatFileItemReader csvFlatFileItemReader(){
    FlatFileItemReader<Player> itemReader = new FlatFileItemReader<>();
    itemReader.setResource(new ClassPathResource("players.csv"));
    DefaultLineMapper<Player> lineMapper = new DefaultLineMapper<>();
    lineMapper.setLineTokenizer(new DelimitedLineTokenizer());
    lineMapper.setFieldSetMapper(new PlayerFieldSetMapper());
    itemReader.setLineMapper(lineMapper);
    itemReader.open(new ExecutionContext());
    return itemReader;
  }

  @Override
  public void afterPropertiesSet() throws Exception {
//    getPlayers();
  }

  protected static class PlayerFieldSetMapper implements FieldSetMapper<Player> {
    @Override
    public Player mapFieldSet(FieldSet fieldSet) {
      Player player = new Player();

      player.setID(fieldSet.readString(0));
      player.setLastName(fieldSet.readString(1));
      player.setFirstName(fieldSet.readString(2));
      player.setPosition(fieldSet.readString(3));
      player.setBirthYear(fieldSet.readInt(4));
      player.setDebutYear(fieldSet.readInt(5));
      return player;
    }
  }
}
